package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.ILegalSalesmanClockOnService;
import com.mdd.admin.validate.LegalSalesmanClockOnSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalSalesmanClockOnListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalSalesmanClockOn;
import com.mdd.common.mapper.LegalSalesmanClockOnMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

/**
 * 销售打卡记录实现类
 * @author LikeAdmin
 */
@Service
public class LegalSalesmanClockOnServiceImpl implements ILegalSalesmanClockOnService {
        
    @Resource
    LegalSalesmanClockOnMapper legalSalesmanClockOnMapper;

    /**
     * 销售打卡记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalSalesmanClockOnListedVo>
     */
    @Override
    public PageResult<LegalSalesmanClockOnListedVo> list(PageValidate pageValidate, LegalSalesmanClockOnSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        MPJQueryWrapper<LegalSalesmanClockOn> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(LegalSalesmanClockOn.class)
                .select("t.`id`, t2.`nickname`")
                .leftJoin("`business_user` t2 on t2.`id` = t.`user_id`")
                .orderByDesc("t.`id`");
        legalSalesmanClockOnMapper.setSearch(mpjQueryWrapper, searchValidate, new String[]{
                "=:userId@t2.`id`:str",
        });

        IPage<LegalSalesmanClockOnListedVo> iPage = legalSalesmanClockOnMapper.selectJoinPage(
                new Page<>(page, limit), LegalSalesmanClockOnListedVo.class, mpjQueryWrapper);

        for (LegalSalesmanClockOnListedVo vo : iPage.getRecords()) {
            vo.setClockStatus("已打卡");
            vo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(vo.getDate())
                    + " " + new SimpleDateFormat("HH:mm:ss").format(vo.getTime()));
        }

        return PageResult.iPageHandle(iPage);
    }

}
